package com.imedway.ihe.dtz.xds.service.impl;

import java.net.URL;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.Addressing;
import javax.xml.ws.soap.MTOM;
import javax.xml.ws.soap.SOAPBinding;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetResponseType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryErrorList;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.openehealth.ipf.commons.ihe.xds.iti42.Iti42PortType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imedway.ihe.dtz.xds.repository.camel.Iti41Repository_CAMEL;
import com.imedway.ihe.dtz.xds.repository.camel.Iti43Repository_CAMEL;
import com.imedway.ihe.dtz.xds.service.XdsRegistryService;
import com.imedway.ihe.dtz.xds.service.XdsRepositoryService;
import com.imedway.ihe.dtz.xds.util.JaxbMapperUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@MTOM
@WebService
@Service
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@Addressing(required = true)
public class XdsRepository implements XdsRepositoryService {

	@Autowired
	private Iti41Repository_CAMEL iti41Repository_CAMEL;
	
	@Autowired
	private Iti43Repository_CAMEL iti43Repository_CAMEL;
	
    @WebMethod
    @Override
    public RetrieveDocumentSetResponseType documentRepositoryRetrieveDocumentSet(RetrieveDocumentSetRequestType body) {
		log.info(JaxbMapperUtils.convertToXml(body, "UTF-8", true));    
		RetrieveDocumentSetResponseType retrieveDocumentSetResponseType = new RetrieveDocumentSetResponseType();
//		Iti43Repository iti43Repository =new Iti43Repository();
//		retrieveDocumentSetResponseType=iti43Repository.run(body);
		
		retrieveDocumentSetResponseType=iti43Repository_CAMEL.run(body);
		log.info(JaxbMapperUtils.convertToXml(retrieveDocumentSetResponseType, "UTF-8", true));   
		return retrieveDocumentSetResponseType;
    }
    

	@WebMethod
	@Override
	public RegistryResponseType documentRepositoryProvideAndRegisterDocumentSetB(
			@WebParam ProvideAndRegisterDocumentSetRequestType body) {
		log.debug("-------Iti41PortTypeImpl.documentRepositoryProvideAndRegisterDocumentSetB-------");
		log.debug(JaxbMapperUtils.convertToXml(body, "UTF-8", true));
		RegistryResponseType registryResponseType=new RegistryResponseType();
//		Iti41Repository iti41Repository = new Iti41Repository();
		registryResponseType = iti41Repository_CAMEL.run(body);
//		if("urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success".equals(registryResponseType.getStatus())) {		
		RegistryResponseType registry42ResponseType =iti42Regitry(body.getSubmitObjectsRequest());
		RegistryErrorList registryErrorList =registry42ResponseType.getRegistryErrorList();
		if(registryErrorList!=null) {
			RegistryErrorList registry41ErrorList = registryResponseType.getRegistryErrorList();
			List<RegistryError> registry41Errors = registry41ErrorList.getRegistryError();
			List<RegistryError> registryErrors = registryErrorList.getRegistryError();
			for(RegistryError reError:registryErrors) {
				registry41Errors.add(reError);
			}
			registryResponseType.setRegistryErrorList(registry41ErrorList);
		}
		
//		};
		log.debug(JaxbMapperUtils.convertToXml(registryResponseType, "UTF-8", true));
		return registryResponseType;
	}

	private RegistryResponseType  iti42Regitry(SubmitObjectsRequest submitObjectsRequest) {
		URL wsdlUrl;
		RegistryResponseType registryResponseType = new RegistryResponseType();
		try {
//			wsdlUrl = new URL("http://localhost:8080/services/iti42?wsdl");
//			QName serviceName = new QName("http://service.ihe.imedway.com/", "Iti42PortTypeImplService");
			wsdlUrl = new URL("http://localhost:8080/services/xdsRegistryService?wsdl");
			QName serviceName = new QName("http://impl.service.xds.dtz.ihe.imedway.com/", "XdsRegistryService");
			javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdlUrl, serviceName);
			XdsRegistryService myWebService = service.getPort(XdsRegistryService.class);
			registryResponseType=myWebService.documentRegistryRegisterDocumentSetB(submitObjectsRequest);
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		return registryResponseType;
	}
	

}
